/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byzantine.views;

import byzantine.ByzantineModel;
import byzantine.Message;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

/**
 *
 * @author Ryhan
 */
public class ReceivedMessageView extends JPanel implements View {

    private static final String receivedMessagesString = "Received Messages";
    private static final String decisionMessagesString = "Decision Messages";

    //private int width;
    //private int height;

    private int width = 250;
    private int height = 175;

    private ByzantineModel model;
    private JLabel panelName;
    private JTextPane samplePane;
    private JScrollPane scrollPane;

    public ReceivedMessageView(ByzantineModel model) {
        this.model = model;
        model.addObserver(this);
/*
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension screenSize = t.getScreenSize();
        width = (int)((screenSize.width - 10) * 0.185);
        height = (int)((screenSize.height - 100) * 0.3935);
*/
        initializeView();
    }

    private void initializeView() {

        // Initialize the components
        panelName = new JLabel(receivedMessagesString);

        samplePane = new JTextPane();
        samplePane.setFont(new Font("Monospaced", Font.PLAIN, 12));
        samplePane.setEditable(false);

        scrollPane = new JScrollPane(samplePane);
        scrollPane.setPreferredSize(new Dimension(width, (int) (height * .85)));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setWheelScrollingEnabled(true);

        this.setBackground(Color.LIGHT_GRAY);
        this.add(panelName);
        this.add(scrollPane);

        setBorder(BorderFactory.createEtchedBorder());
    }

    private void populateMessages() {
        StyledDocument doc = samplePane.getStyledDocument();
        if (model.isSimulationRunning()) {
            Integer step = model.getExaminedStep();
            if (step < model.getNumRounds() && model.getSelectedGeneral() != null) {
                panelName.setText(receivedMessagesString);
                try {
                    doc.remove(0, doc.getLength());

                    ArrayList<Message> receivedMessages = model.getSelectedGeneral().getReceivedMessages(step);
                    
                    for (int i = 0; i < receivedMessages.size(); i++) {
                        doc.insertString(doc.getLength(), receivedMessages.get(i).toString() + "\n", null);
                    }
                    samplePane.setCaretPosition(0);
                } catch (Exception e) {
                    //e.printStackTrace(System.err);
                }
            } else if (step >= model.getNumRounds() && model.getSelectedGeneral() != null) {
                panelName.setText(decisionMessagesString);
                try {
                    doc.remove(0, doc.getLength());

                    ArrayList<Message> decisionMessages = model.getSelectedGeneral().getDecisionMessages(model.getNumSteps() - step - 1);

                    for (int i = 0; i < decisionMessages.size(); i++) {
                        doc.insertString(doc.getLength(), decisionMessages.get(i).toString() + "\n", null);
                    }
                    samplePane.setCaretPosition(0);
                } catch (Exception e) {
                    //e.printStackTrace(System.err);
                }
            }
        } else {
            try {
                doc.remove(0, doc.getLength());
            } catch (Exception e) {
                //e.printStackTrace(System.err);
            }
        }
    }

    public void valueChanged() {
        if (model.hasSimulationStatusChanged()
                || model.hasSelectedGeneralChanged()
                || model.hasExaminedStepChanged()) {
            populateMessages();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);

    }
}
